package org.lmj.dos.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.ImmutableMap;
import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.pojo.*;
import org.lmj.dos.mapper.*;
import org.lmj.dos.web.resultdo.ProductDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/admin")
@RestController
public class AdminController {

    @Autowired
    Category1Mapper category1Mapper;
    @Autowired
    Category2Mapper category2Mapper;
    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductImageMapper productImageMapper;
    @Autowired
    ProductAttrValueMapper productAttrValueMapper;

    @Autowired
    OrderMapper orderMapper;

    @RequestMapping("/category1/get")
    public ServerResponse get1(){
        List<Category1Entity> category1Entities = category1Mapper.selectList(new QueryWrapper<Category1Entity>());
        return ServerResponse.success(category1Entities);
    }

    @RequestMapping("/category2/get/{cat1Id}")
    public ServerResponse get2(@PathVariable("cat1Id") Integer cate1Id){
        QueryWrapper<Category2Entity> wrapper = new QueryWrapper<>();
        wrapper.in("category1_id",cate1Id);
        List<Category2Entity> category1Entities = category2Mapper.selectList(wrapper);
        return ServerResponse.success(category1Entities);
    }


    @RequestMapping("/baseAttrInfo/get/{cat2Id}")
    public ServerResponse baseAttrInfo(@PathVariable("cat2Id") Integer cate2Id){
        QueryWrapper<BaseAttrInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.in("category2_id",cate2Id);
        List<BaseAttrInfoEntity> baseAttrInfoEntityList = baseAttrInfoMapper.selectList(wrapper);
        return ServerResponse.success(baseAttrInfoEntityList);
    }

    @RequestMapping("/baseAttrValue/get/{attrId}")
    public ServerResponse baseAttrValue(@PathVariable("attrId") Integer attrId){
        QueryWrapper<BaseAttrValueEntity> wrapper = new QueryWrapper<>();
        wrapper.in("attr_id",attrId);
        List<BaseAttrValueEntity> baseAttrInfoEntityList = baseAttrValueMapper.selectList(wrapper);
        return ServerResponse.success(baseAttrInfoEntityList);
    }


    @RequestMapping("/saveAttrInfo")
    @ResponseBody
    public ServerResponse saveAttrInfo(@RequestBody BaseAttrInfoEntity pmsBaseAttrInfo){
            Integer id = pmsBaseAttrInfo.getId();
            if(id == null){
                //id为空,直接插入数据
                //保存属性
                baseAttrInfoMapper.insert(pmsBaseAttrInfo);
                //保存属性值
                List<BaseAttrValueEntity> attrValueList = pmsBaseAttrInfo.getAttrValueList();
                for(BaseAttrValueEntity pmsBaseAttrValue : attrValueList){
                    pmsBaseAttrValue.setAttrId(pmsBaseAttrInfo.getId());
                    baseAttrValueMapper.insert(pmsBaseAttrValue);
                }
            }else{
                baseAttrInfoMapper.update(pmsBaseAttrInfo,new UpdateWrapper<BaseAttrInfoEntity>().eq("id",id));
                //id不为空，修改数据：先删除后插入
                baseAttrValueMapper.delete(new QueryWrapper<BaseAttrValueEntity>().eq("attr_id",id));
                List<BaseAttrValueEntity> attrValueList = pmsBaseAttrInfo.getAttrValueList();
                for(BaseAttrValueEntity pmsBaseAttrValue : attrValueList){
                    pmsBaseAttrValue.setAttrId(pmsBaseAttrInfo.getId());
                    baseAttrValueMapper.insert(pmsBaseAttrValue);
                }

            }
        return ServerResponse.success();
    }

    @RequestMapping("/getSpuList/{cat2Id}")
    public ServerResponse getSpuList(@PathVariable("cat2Id") Integer cat2Id){
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.in("category_id",cat2Id);
        List<ProductEntity> baseAttrInfoEntityList = productMapper.selectList(wrapper);
        return ServerResponse.success(baseAttrInfoEntityList);
    }

    @RequestMapping("/attrInfoValueAll/{cat2Id}")
    public ServerResponse attrInfoValueAll(@PathVariable("cat2Id") Integer cate2Id){
        QueryWrapper<BaseAttrInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.in("category2_id",cate2Id);
        List<BaseAttrInfoEntity> baseAttrInfoEntityList = baseAttrInfoMapper.selectList(wrapper);
        List<BaseAttrInfoEntity> res = baseAttrInfoEntityList.stream().map(attrInfo -> {
            attrInfo.setAttrValueList(baseAttrValueMapper.selectByMap(ImmutableMap.of("attr_id", attrInfo.getId())));
            return attrInfo;
        }).collect(Collectors.toList());
        return ServerResponse.success(res);
    }



    @GetMapping("/getByProductId/{productId}")
    public ServerResponse productsList(@PathVariable("productId") Integer productId) throws IOException {
        QueryWrapper<ProductImageEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ProductImageEntity::getProductId,productId);
        List<ProductImageEntity> productImageEntityList = productImageMapper.selectList(wrapper);
        return ServerResponse.success(productImageEntityList);
    }

    @RequestMapping("/saveProduct")
    public ServerResponse saveProduct(@RequestBody ProductDto productDto) throws IOException {
        ProductEntity productEntity = new ProductEntity();
        BeanUtils.copyProperties(productDto,productEntity);
        productMapper.insert(productEntity);
        Integer productId = productEntity.getId();
        List<ProductAttrValueEntity> skuAttrValueList = productDto.getSkuAttrValueList();
        for (ProductAttrValueEntity pav:skuAttrValueList) {
            pav.setProductId(productId);
            productAttrValueMapper.insert(pav);
        }
        List<ProductImageEntity> imgs = productDto.getImgs();
        for (ProductImageEntity pi:imgs) {
            pi.setProductId(productId);
            productImageMapper.insert(pi);
        }
        return ServerResponse.success();
    }


    @RequestMapping("/getProductAll/{productId}")
    public ServerResponse getProductAll(@PathVariable("productId")Integer productId) throws IOException {
        ProductDto productDto=new ProductDto();
        ProductEntity productEntity = productMapper.selectById(productId);
        productDto.setCategoryId(productEntity.getCategoryId());
        productDto.setId(productEntity.getId());
        productDto.setPrice(productEntity.getPrice());
        productDto.setMainImage(productEntity.getMainImage());
        productDto.setName(productEntity.getName());
        productDto.setSubtitle(productEntity.getSubtitle());
        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
        wrapper.in("product_id",productEntity.getId());
        productDto.setSkuAttrValueList(productAttrValueMapper.selectList(wrapper));
        QueryWrapper<ProductImageEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.in("product_id",productEntity.getId());
        List<ProductImageEntity> productImageEntityList = productImageMapper.selectList(wrapper1);
        productDto.setImgs(productImageEntityList);
        return ServerResponse.success(productDto);
    }

    @RequestMapping("/saveCategory1")
    public ServerResponse getProductAll(@RequestBody Category1Entity category1Entity) throws IOException {
        QueryWrapper<Category1Entity> wrapper = new QueryWrapper<>();
        wrapper.eq("name",category1Entity.getName());
        Category1Entity category1Entity1 = category1Mapper.selectOne(wrapper);
        if (category1Entity1!=null){
            return ServerResponse.success("该类别已存在，请勿重复添加");
        }
        category1Mapper.insert(category1Entity);
        return ServerResponse.success("success");
    }
    @RequestMapping("/saveCategory2")
    public ServerResponse getProductAll(@RequestBody Category2Entity category2Entity) throws IOException {
        Category2Entity category2Entity1 = new Category2Entity();
       // category1Entity.setName(name);
        QueryWrapper<Category2Entity> wrapper = new QueryWrapper<>();
        wrapper.eq("name",category2Entity.getName());
        wrapper.eq("category1_id",category2Entity.getCategory1Id());
        Category2Entity db = category2Mapper.selectOne(wrapper);
        if (db!=null){
            return ServerResponse.success("该类别已存在，请勿重复添加");
        }
        category2Mapper.insert(category2Entity);
        return ServerResponse.success("success");
    }

    /**
     * 查询所有订单
     */
    @RequestMapping("/getAllOrder")
    public ServerResponse getAllOrder(String keyword) throws IOException {
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)&&!keyword.equals("null")){
            wrapper.like("order_no",keyword);
        }
        List<OrderEntity> orderEntities = orderMapper.selectList(wrapper);
        return ServerResponse.success(orderEntities);
    }
}
